//
//  OneDayDetailTableView.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/6.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

protocol OneDayDetailDelegate {
    func showInstantAddView(withDate: NSDate, isWholeDayEvent: Bool )
}

class OneDayDetailTableView: UICollectionViewCell , UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var detailTableView: UITableView!
    
    let emptyCellHeight: CGFloat = 26.0
    let instantViewHeight: CGFloat = 48.0
    let eventCellHeight: CGFloat = 50.0
    
    var date: NSDate!
    
    var oneDaydelegate: OneDayDetailDelegate! = nil
    
    var eventList = [EventInDayView](){
        didSet {
            self.detailTableView.reloadData()
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        
        detailTableView.delegate = self
        detailTableView.dataSource = self
        detailTableView.bounces = false
        
        detailTableView.registerNib(UINib.init(nibName: "DayEmptyTimeCell", bundle: nil), forCellReuseIdentifier: "DayEmptyTimeCell")
        detailTableView.registerNib(UINib.init(nibName: "DayDetailViewCell", bundle: nil), forCellReuseIdentifier: "DayDetailViewCell")
        
        // Initialization code
    }
    
    //MARK: - UITableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return eventList.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let row = indexPath.row
        let event = eventList[row]
        
        if  event.isEmpty{
            let cell = detailTableView.dequeueReusableCellWithIdentifier("DayEmptyTimeCell") as! DayEmptyTimeCell
            cell.timeLabel.text = String(format: "%02d:00", event.emptyTime)
            return cell
        }else{
            
            let cell = detailTableView.dequeueReusableCellWithIdentifier("DayDetailViewCell") as! DayDetailViewCell
            cell.event = event
            
            return cell
        }
        
    }
    
    //MARK: - UITableViewDelegate
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = NSBundle.mainBundle().loadNibNamed("InstantAddView", owner: self, options: nil).last as! InstantAddView
        headerView.backgroundView?.backgroundColor = UIColor.clearColor()
        headerView.contentView.backgroundColor = UIColor.clearColor()
        headerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.showInstantViewWithWholeDayEvent)))
        
        return headerView
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return instantViewHeight
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let row = indexPath.row
        let event = eventList[row]
        if event.isEmpty {
            //TODO 计算cell对应的时间
            let components = NSDateComponents()
            components.hour = event.emptyTime
            components.second = 1
            let date = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: self.date, options: .MatchStrictly)
            self.showInstantView(date!)
        }
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let row = indexPath.row
        if  eventList[row].isEmpty{
            return emptyCellHeight
        }else{
            return eventCellHeight
        }
    
    }
    
    func showInstantView(withDate: NSDate) {
        self.oneDaydelegate.showInstantAddView(withDate, isWholeDayEvent: false)
    }
    
    func showInstantViewWithWholeDayEvent() {
        self.oneDaydelegate.showInstantAddView(self.date!, isWholeDayEvent: true)
    }
}
